package mat;
import types.MPic;

import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.Vector;

import javax.imageio.ImageIO;
import javax.swing.JPanel;



class MPicPanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public MPicPanel(){
		
		setOpaque(false);
	}
	
	public void pushNew(MPic obj){
		mp.add(obj);		
	}
	
	public void setCoordiante(int index, int x, int y){
		mp.get(index).setXaxis(doubleToInt(x));
		mp.get(index).setYaxis(doubleToInt(y));
	}
	
	public void paintComponent(Graphics g){
		super.paintComponent(g);
		for(int i=0; i< mp.size(); i++){
			xaxis = doubleToInt(mp.get(i).getXaxis());
			yaxis = doubleToInt(mp.get(i).getYaxis());
			Image tmp_img = mp.get(i).getImage();
			System.out.println(mp.get(i).path+"+"+mp.get(i).getId());
			int picwid = (int) mp.get(i).getWidth();
			int picheight = (int) mp.get(i).getHeight();
			int picRightBorder = xaxis + picwid;
			int picDownBorder = yaxis + picheight;
			
			//adjust the size of pics
			if(picRightBorder > MCanvas.PhoneScreenWidth){
				picwid = MCanvas.PhoneScreenWidth - xaxis;
			}			
			if(picDownBorder > MCanvas.PhoneScreenHeight){
				picheight = MCanvas.PhoneScreenHeight - yaxis;
			}
			if(xaxis < 0){
				xaxis = 0;
			}			
			if(yaxis < 0){
				yaxis = 0;
			}
		
			
			g.drawImage(mp.get(i).getImage(), xaxis, yaxis, picwid,picheight, null);
			
			try {
				paintPhone(g);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("PIC >> "+ i+"==:"+xaxis+","+yaxis);
		}
	}
	
	public void paintPhone(Graphics g) throws IOException{
		/*
		//top edge
		g.fillRect(0, 0, MCanvas.PhoneWidth, 10);
        //left edge
		g.fillRect(0, 0, 10, MCanvas.PhoneHeight);
		//down edge
		g.fillRect(0, MCanvas.PhoneHeight - 200, MCanvas.PhoneWidth, 200);
		//right edge
		g.fillRect(MCanvas.PhoneWidth - 15, 0, 15, MCanvas.PhoneHeight);
//		g.drawImage(ImageIO.read(new File("F:\\buttons.jpg")),0,MCanvas.PhoneHeight - 200, MCanvas.PhoneWidth -10, 170, null);
 * 		*/
		
	}
	
	private int doubleToInt(double d){
		Double t = new Double(d);
		return t.intValue();
	}
	
	private Vector<MPic> mp = new Vector<MPic>();
	private int xaxis;
	private int yaxis;
}